package com.zfh;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * 1.直接实现Servlet接口，编写用户需要的Serlvet【类，组件，控制器】<br>
 * <li> Servlet接口
 * <li> GenericServlet抽象类
 * <li> HttpServlet类<br>
 * 2.采用了注解的配置方式，最简单的注解，只给出url-pattern即可，建议统一用小写<br>
 * 3.所有的命名建议和要求：见名知义，英文
 * 
 */
public class ServletDemo02 implements Servlet{

	public static void main(String[] args) throws ServletException, IOException {
		// 用户自己调用这些方法，但没有意义
		ServletDemo02 myServlet = new ServletDemo02();
		myServlet.init(null);
		myServlet.getServletConfig();	// 其实没有意义
		myServlet.getServletInfo();		// 其实没有意义
		myServlet.service(null, null);
		myServlet.destroy();
	}

	@Override
	public void destroy() {
		System.out.println("call destroy");
	}

	@Override
	public ServletConfig getServletConfig() {
		ServletConfig servletConfig = new ServletConfig() {
			
			@Override
			public String getServletName() {
				return "servletName";
			}
			
			@Override
			public ServletContext getServletContext() {
				ServletContext servletContext = null;
				return servletContext;
			}
			
			@Override
			public Enumeration<String> getInitParameterNames() {
				Enumeration<String> initParams = new Enumeration<String>() {
					
					@Override
					public String nextElement() {
						return "only one string for initParams";
					}
					
					@Override
					public boolean hasMoreElements() {
						return false;
					}
				};
				return initParams;
			}
			
			@Override
			public String getInitParameter(String arg0) {
				return "get init params";
			}
		};
		return null;
	}

	@Override
	public String getServletInfo() {
		return "servlet infomation";
	}

	/**
	 *	初始化方法
	 */
	@Override
	public void init(ServletConfig arg0) throws ServletException {
		System.out.println("call init");
	}

	/**
	 *	服务方法，这是用户【程序员的工作位置】
	 */
	@Override
	public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
		System.out.println("call service");
	}

}
